package com.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MySqlSession {
    private static String resources = "mybatis.xml";
    private static SqlSession sqlSession = null;

    public static SqlSession createSqlSession(){
        try {
            Reader asReader = Resources.getResourceAsReader(resources);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(asReader);
             sqlSession = sqlSessionFactory.openSession();
            return sqlSession;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }

    public static void closeSqlSession(){
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

}
